package com.yuke.cloud.service.wmc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.service.api.pmc.dto.ProdCategoryDto;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecApiDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.wmc.dao.WmsStockOutMapper;
import com.yuke.cloud.service.wmc.dao.WmsStorageMapper;
import com.yuke.cloud.service.wmc.dto.WmsExcelImportDto;
import com.yuke.cloud.service.wmc.dto.WmsStockOutDto;
import com.yuke.cloud.service.wmc.entity.WmsStockOut;
import com.yuke.cloud.service.wmc.entity.WmsStorage;
import com.yuke.cloud.service.wmc.service.WmsStockOutService;
import com.yuke.cloud.service.wmc.service.WmsStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zys on 2018/12/06.
 */
@Service
@Transactional
public class WmsStockOutServiceImpl extends BaseService<WmsStockOut> implements WmsStockOutService {
    @Resource
    private WmsStockOutMapper wmsStockOutMapper;
    @Resource
    private WmsStorageService wmsStorageService;
    @Resource
    private PmcFeignApi pmcFeignApi;
    @Resource
    private WmsStorageMapper wmsStorageMapper;

    @Override
    public List<WmsStockOutDto> getWmsStockOutByCondition(WmsStockOutDto wmsStockOutDto) {
        return wmsStockOutMapper.getWmsStockOutByCondition(wmsStockOutDto);
    }

    @Override
    public Integer stockOutPurchase(List<WmsStockOut> wmsStockOutList) {
        for (WmsStockOut wmsStockOut: wmsStockOutList){

            if (wmsStockOut.getOutNumber() < 1){
                continue;
            }
            wmsStockOut.setOutboundTime(new Date());
            wmsStockOut.setUpdateTime(new Date());
           //插入出库记录
            wmsStockOutMapper.insert(wmsStockOut);

            //更新wmsStorage表内库存数
            WmsStorage wmsStorage = new WmsStorage();
            wmsStorage.setSpecId(wmsStockOut.getSpecId());
            wmsStorage.setWarehouseId(wmsStockOut.getWarehouseId());
            List<WmsStorage> select = wmsStorageService.select(wmsStorage);
            wmsStorage = select.get(0);
            Integer number = wmsStorage.getNumber()-wmsStockOut.getOutNumber();
            wmsStorage.setNumber(number);//只削减库存
            wmsStorage.setUpdateTime(new Date());
            Integer update = wmsStorageService.update(wmsStorage);
            if (update.equals(0)){
                return 0;
            }
        }
        return 1;
    }

    @Override
    public PageInfo<WmsStockOutDto> countBySpec(WmsStockOutDto wmsStockOutDto, Integer page, Integer size) {
        //分类
        if (wmsStockOutDto.getParentId() != null || wmsStockOutDto.getCategoryId() != null) {
            ProdCategoryDto prodCategoryDto = new ProdCategoryDto();
            prodCategoryDto.setParentId(wmsStockOutDto.getParentId());
            prodCategoryDto.setCategoryId(wmsStockOutDto.getCategoryId());
            List<Long> leaf = pmcFeignApi.findLeaf(prodCategoryDto);
            wmsStockOutDto.setCategoryIdList(leaf);
            wmsStockOutDto.setCategoryId(null);
        }
        PageHelper.startPage(page, size);

        List<WmsStockOutDto> list = wmsStockOutMapper.countOutBySpec(wmsStockOutDto);

        if (list==null||list.size()==0){
            return null;
        }

        PageInfo<WmsStockOutDto> result = new PageInfo<>(list);

        List<Long> collect = list.stream().map(WmsStockOutDto::getCategoryId).collect(Collectors.toList());

        List<ProdSpecApiDto> parent = pmcFeignApi.findParent(collect);

        if (parent!=null&&parent.size()>0){
            for (WmsStockOutDto stockOutDto : list) {
                for (ProdSpecApiDto prodSpecApiDto : parent) {
                    if (stockOutDto.getCategoryId().equals(prodSpecApiDto.getCategoryId())){
                        stockOutDto.setParentId(prodSpecApiDto.getParentId());
                        stockOutDto.setParentName(prodSpecApiDto.getParentName());
                        stockOutDto.setCategoryName(prodSpecApiDto.getCategoryName());
                    }
                }
            }
        }

        result.setList(list);
        return result;
    }

    @Override
    public List<WmsStockOutDto> excelStockOut(WmsExcelImportDto wmsExcelImportDto) {

        List<String> specCode = wmsExcelImportDto.getWmsStockOutDto().stream().map(WmsStockOutDto::getSpecCode).collect(Collectors.toList());

        //1.检查商品信息 返回存在条形码的商品
        List<ProdSpecApiDto> list = pmcFeignApi.checkSpeCode(specCode);

        if (list == null || list.size() < 1) {
            return wmsExcelImportDto.getWmsStockOutDto();
        }

        //需要查询的值
        List<WmsStockOutDto> wmsStockOut = new ArrayList<>();


        for (int i = wmsExcelImportDto.getWmsStockOutDto().size()-1; i>=0; i--) {
            for (ProdSpecApiDto prodSpecApiDto : list) {
                WmsStockOutDto wmsStockOutDto = wmsExcelImportDto.getWmsStockOutDto().get(i);
                if (wmsStockOutDto.getSpecCode().equals(prodSpecApiDto.getSpecCode())){
                    //需要查找的list
                    WmsStockOutDto dto = new WmsStockOutDto();

                    dto.setSpecCode(wmsStockOutDto.getSpecCode());
                    dto.setSpecId(prodSpecApiDto.getSpecId());
                    dto.setOutNumber(wmsStockOutDto.getOutNumber());
                    dto.setOutboundTime(new Date());
                    dto.setUpdateId(prodSpecApiDto.getUpdateId());
                    dto.setPrice(wmsStockOutDto.getPrice());
                    wmsStockOut.add(dto);

                    //删除可找到的speccode的dto
                    wmsExcelImportDto.getWmsStockOutDto().remove(wmsStockOutDto);
                }
            }
        }


        //wmsStorage仓库表增减库存
        for (WmsStockOutDto wmsStockOutDto : wmsStockOut) {
            WmsStorage wmsStorage = new WmsStorage();
            wmsStorage.setWarehouseId(wmsExcelImportDto.getWarehouseId());
            wmsStorage.setSpecId(wmsStockOutDto.getSpecId());
            //查
            List<WmsStorage> select = wmsStorageService.select(wmsStorage);

            if (select==null||select.size()<=0){
                wmsExcelImportDto.getWmsStockOutDto().add(wmsStockOutDto);
                break;
            }

            WmsStorage before=null;

            if (select!=null&&select.size()>0){
                before = select.get(0);
            }

            if(before.getNumber()==null){
                before.setNumber(0);
            }
            if (before.getStockCount()==null){
                before.setStockCount(0);
            }
            //对number字段操作
            if (before.getNumber()-before.getLockNumber()-wmsStockOutDto.getOutNumber()<=0){
                before.setNumber(before.getLockNumber());
            }else {
                before.setNumber(before.getNumber()-wmsStockOutDto.getOutNumber());
            }
            //对stock_count字段操作
            if (before.getStockCount()-before.getLockNumber()-wmsStockOutDto.getOutNumber()<=0){
                before.setSaleCount(Long.valueOf(before.getLockNumber()));
            }else {
                before.setSaleCount((long) (before.getNumber()-wmsStockOutDto.getOutNumber()));
            }

            //更新WmsStorage
            Integer i = wmsStorageService.update(before);

            Integer integer = 0;
            if (i>0) {
                //添加出库记录
                WmsStockOut stockOut = new WmsStockOut();
                stockOut.setSpecId(wmsStockOutDto.getSpecId());
                stockOut.setOutNumber(wmsStockOutDto.getOutNumber());
                stockOut.setUpdateId(wmsExcelImportDto.getUpdateId());
                stockOut.setOutboundTime(new Date());
                stockOut.setWarehouseId(wmsExcelImportDto.getWarehouseId());
                stockOut.setOutType((byte) 6);
                stockOut.setOutStatus((byte) 1);
                stockOut.setProductId(before.getProductId());
                stockOut.setOutPrice(wmsStockOutDto.getPrice());
                stockOut.setIsDelete(false);

                integer = wmsStockOutMapper.insert(stockOut);
            }

            if (integer == 0){
                //未成功的进行返回
                wmsExcelImportDto.getWmsStockOutDto().add(wmsStockOutDto);
            }

        }

        return wmsExcelImportDto.getWmsStockOutDto();
    }



}
